﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;

namespace WxTenpay.Common
{
   
    public class Net
    {
        #region Ip(获取Ip)
        /// <summary>
        /// 获取Ip
        /// </summary>
        public static string Ip
        {
            get
            {
                var remoteIp = GetWebProxyRealIp() ?? HttpContext.Current.Connection.RemoteIpAddress.ToString();
                if (remoteIp == "::1" || remoteIp == "")
                {
                    remoteIp = "127.0.0.1";
                }
                return remoteIp;
            }
        }
        /// <summary>
        /// 获取Web代理真实IP
        /// </summary>
        /// <returns></returns>
        private static string GetWebProxyRealIp()
        {
            var request = HttpContext.Current.Request;
            string ip = request.Headers["x-forwarded-for"];
            if (string.IsNullOrEmpty(ip) || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase))
            {
                ip = request.Headers["Proxy-Client-IP"];
            }
            if (string.IsNullOrEmpty(ip) || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase))
            {
                ip = request.Headers["WL-Proxy-Client-IP"];
            }
            if (string.IsNullOrEmpty(ip))
            {
                return string.Empty;
            }
            return ip;
        }
        #endregion

        #region 获取mac地址
        /// <summary>
        /// 返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。
        /// </summary>
        /// <returns></returns>
        public static NetworkInterface[] NetCardInfo()
        {
            return NetworkInterface.GetAllNetworkInterfaces();
        }
        ///<summary>
        /// 通过NetworkInterface读取网卡Mac
        ///</summary>
        ///<returns></returns>
        public static List<string> GetMacByNetworkInterface()
        {
            List<string> macs = new List<string>();
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces)
            {
                macs.Add(ni.GetPhysicalAddress().ToString());
            }
            return macs;
        }
        #endregion

        #region Ip城市(获取Ip城市)
        /// <summary>
        /// 获取IP地址信息
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        //public static string GetLocation(string ip)
        //{
        //    string res = "";
        //    try
        //    {
        //        string url = "http://apis.juhe.cn/ip/ip2addr?ip=" + ip + "&dtype=json&key=b39857e36bee7a305d55cdb113a9d725";
        //        res = HttpMethods.HttpGet(url);
        //        var resjson = res.ToObject<objex>();
        //        if (resjson.result != null)
        //        {
        //            res = resjson.result.area + " " + resjson.result.location;
        //        }
        //        else
        //        {
        //            res = "";
        //        }
        //    }
        //    catch
        //    {
        //        res = "";
        //    }
        //    if (!string.IsNullOrEmpty(res))
        //    {
        //        return res;
        //    }
        //    try
        //    {
        //        string url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" + ip + "&resource_id=6006&ie=utf8&oe=gbk&format=json";
        //        res = HttpMethods.HttpGet(url, Encoding.GetEncoding("GBK"));
        //        var resjson = res.ToObject<obj>();
        //        res = resjson.data[0].location;
        //    }
        //    catch
        //    {
        //        res = "";
        //    }
        //    return res;
        //}
        /// <summary>
        /// 百度接口
        /// </summary>
        public class obj
        {
            public List<dataone> data { get; set; }
        }
        public class dataone
        {
            public string location { get; set; }
        }
        /// <summary>
        /// 聚合数据
        /// </summary>
        public class objex
        {
            public string resultcode { get; set; }
            public dataoneex result { get; set; }
            public string reason { get; set; }
            public string error_code { get; set; }
        }
        public class dataoneex
        {
            public string area { get; set; }
            public string location { get; set; }
        }
        #endregion

        #region Browser(获取浏览器信息)
   
        /// <summary>
        /// 获取浏览器header
        /// </summary>
        public static IHeaderDictionary Headers
        {
            get
            {
                return HttpContext.Current.Request.Headers;
            }
        }
    
        #endregion

        #region 请求地址
        /// <summary>
        /// 请求地址
        /// </summary>
        public static string RequestURI
        {
            get
            {
                if (HttpContext.Current == null)
                    return string.Empty;
                return HttpContext.Current.Request.Path;
            }
        }
        #endregion

        #region 请求方法
        /// <summary>
        /// 请求方法
        /// </summary>
        public static string RequestMethod
        {
            get
            {
                if (HttpContext.Current == null)
                    return string.Empty;
                return HttpContext.Current.Request.Method;
            }
        }
        #endregion

       
    }
}
